Comparing version 0.0.1-alpha-15 to 0.0.1-alpha-16
@@ -109,16 +109,3 @@ 'use strict'; | ||
let error = null; | ||
let isLoading = false; | ||
let value = isInitialValueAsync ? void 0 : initialValue; | ||
if (isInitialValueAsync) { | ||
status = AsyncStatus.LOADING; | ||
const initialValuePromise = isFunction(initialValue) ? initialValue() : initialValue; | ||
Promise.resolve(initialValuePromise).then((resolvedValue) => { | ||
if (version === 0) { | ||
value = resolvedValue; | ||
version = 1; | ||
status = AsyncStatus.LOADED; | ||
events.changed.emit(resolvedValue); | ||
} | ||
}); | ||
} | ||
const events = { | ||
@@ -131,3 +118,3 @@ changed: new AwaitableEvent(), | ||
const set = async (nextValueOrResolver) => { | ||
const nextVersion = ++version % Number.MAX_SAFE_INTEGER; | ||
const lastVersion = version; | ||
try { | ||
@@ -139,3 +126,3 @@ if (isPromiseOrFunction(nextValueOrResolver)) { | ||
let newValue = isFunction(nextValueOrResolver) ? await nextValueOrResolver(value) : await nextValueOrResolver; | ||
if (version !== nextVersion) { | ||
if (lastVersion !== version) { | ||
return; | ||
@@ -153,6 +140,11 @@ } | ||
} | ||
version++; | ||
await flush(); | ||
}; | ||
const get = () => value; | ||
const getAsync = () => ({ error, isLoading, value }); | ||
const getAsync = () => ({ | ||
error, | ||
isLoading: status === AsyncStatus.LOADING, | ||
value | ||
}); | ||
const getPromise = async () => { | ||
@@ -168,2 +160,6 @@ if (version > 0) { | ||
}; | ||
if (isInitialValueAsync) { | ||
const initialValuePromise = isFunction(initialValue) ? initialValue() : initialValue; | ||
set(initialValuePromise); | ||
} | ||
return { | ||
@@ -170,0 +166,0 @@ events, |
{ | ||
"name": "awai", | ||
"version": "0.0.1-alpha-15", | ||
"version": "0.0.1-alpha-16", | ||
"author": "Yuriy Yakym", | ||
@@ -5,0 +5,0 @@ "description": "State management library", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
87653
800