@tanstack/store 0.1.3 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +47 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/{build/modern → dist/cjs}/index.d.cts +6 -16
- package/{build/modern → dist/esm}/index.d.ts +6 -16
- package/{build/legacy → dist/esm}/index.js +7 -21
- package/dist/esm/index.js.map +1 -0
- package/package.json +17 -13
- package/src/index.ts +5 -27
- package/build/legacy/index.cjs +0 -86
- package/build/legacy/index.cjs.map +0 -1
- package/build/legacy/index.d.cts +0 -30
- package/build/legacy/index.d.ts +0 -30
- package/build/legacy/index.js.map +0 -1
- package/build/modern/index.cjs +0 -84
- package/build/modern/index.cjs.map +0 -1
- package/build/modern/index.js +0 -59
- package/build/modern/index.js.map +0 -1
- /package/src/{__tests__ → tests}/index.test.tsx +0 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class Store {
|
|
4
|
+
constructor(initialState, options) {
|
|
5
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
6
|
+
this._batching = false;
|
|
7
|
+
this._flushing = 0;
|
|
8
|
+
this.subscribe = (listener) => {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
this.listeners.add(listener);
|
|
11
|
+
const unsub = (_b = (_a = this.options) == null ? void 0 : _a.onSubscribe) == null ? void 0 : _b.call(_a, listener, this);
|
|
12
|
+
return () => {
|
|
13
|
+
this.listeners.delete(listener);
|
|
14
|
+
unsub == null ? void 0 : unsub();
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
this.setState = (updater) => {
|
|
18
|
+
var _a, _b, _c;
|
|
19
|
+
const previous = this.state;
|
|
20
|
+
this.state = ((_a = this.options) == null ? void 0 : _a.updateFn) ? this.options.updateFn(previous)(updater) : updater(previous);
|
|
21
|
+
(_c = (_b = this.options) == null ? void 0 : _b.onUpdate) == null ? void 0 : _c.call(_b);
|
|
22
|
+
this._flush();
|
|
23
|
+
};
|
|
24
|
+
this._flush = () => {
|
|
25
|
+
if (this._batching)
|
|
26
|
+
return;
|
|
27
|
+
const flushId = ++this._flushing;
|
|
28
|
+
this.listeners.forEach((listener) => {
|
|
29
|
+
if (this._flushing !== flushId)
|
|
30
|
+
return;
|
|
31
|
+
listener();
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
this.batch = (cb) => {
|
|
35
|
+
if (this._batching)
|
|
36
|
+
return cb();
|
|
37
|
+
this._batching = true;
|
|
38
|
+
cb();
|
|
39
|
+
this._batching = false;
|
|
40
|
+
this._flush();
|
|
41
|
+
};
|
|
42
|
+
this.state = initialState;
|
|
43
|
+
this.options = options;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.Store = Store;
|
|
47
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = () => void\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: () => void\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (updater: TUpdater) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.()\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener()\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"names":[],"mappings":";;AAgBO,MAAM,MAGX;AAAA,EAOA,YAAY,cAAsB,SAA0C;AAN5E,SAAA,gCAAgB;AAGJ,SAAA,YAAA;AACA,SAAA,YAAA;AAOZ,SAAA,YAAY,CAAC,aAAuB;;AAC7B,WAAA,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACN,aAAA,UAAU,OAAO,QAAQ;AACtB;AAAA,MAAA;AAAA,IACV;AAGF,SAAA,WAAW,CAAC,YAAsB;;AAChC,YAAM,WAAW,KAAK;AACtB,WAAK,UAAQ,UAAK,YAAL,mBAAc,YACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAG7B,uBAAK,YAAL,mBAAc,aAAd;AAGA,WAAK,OAAO;AAAA,IAAA;AAGd,SAAA,SAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACd,YAAA,UAAU,EAAE,KAAK;AAClB,WAAA,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AACvB;MAAA,CACV;AAAA,IAAA;AAGH,SAAA,QAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACd;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IAAA;AAxCZ,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AAwCF;;"}
|
|
@@ -1,30 +1,20 @@
|
|
|
1
|
-
type AnyUpdater = (...args: any[]) => any;
|
|
2
|
-
type Listener = (
|
|
3
|
-
priority: Priority;
|
|
4
|
-
}) => void;
|
|
5
|
-
type Priority = 'high' | 'low';
|
|
1
|
+
export type AnyUpdater = (...args: any[]) => any;
|
|
2
|
+
export type Listener = () => void;
|
|
6
3
|
interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
7
4
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
8
5
|
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
9
|
-
onUpdate?: (
|
|
10
|
-
priority: Priority;
|
|
11
|
-
}) => void;
|
|
12
|
-
defaultPriority?: Priority;
|
|
6
|
+
onUpdate?: () => void;
|
|
13
7
|
}
|
|
14
|
-
declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
8
|
+
export declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
15
9
|
listeners: Set<Listener>;
|
|
16
10
|
state: TState;
|
|
17
11
|
options?: StoreOptions<TState, TUpdater>;
|
|
18
12
|
_batching: boolean;
|
|
19
13
|
_flushing: number;
|
|
20
|
-
_nextPriority: null | Priority;
|
|
21
14
|
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
|
|
22
15
|
subscribe: (listener: Listener) => () => void;
|
|
23
|
-
setState: (updater: TUpdater
|
|
24
|
-
priority: Priority;
|
|
25
|
-
}) => void;
|
|
16
|
+
setState: (updater: TUpdater) => void;
|
|
26
17
|
_flush: () => void;
|
|
27
18
|
batch: (cb: () => void) => void;
|
|
28
19
|
}
|
|
29
|
-
|
|
30
|
-
export { AnyUpdater, Listener, Priority, Store };
|
|
20
|
+
export {};
|
|
@@ -1,30 +1,20 @@
|
|
|
1
|
-
type AnyUpdater = (...args: any[]) => any;
|
|
2
|
-
type Listener = (
|
|
3
|
-
priority: Priority;
|
|
4
|
-
}) => void;
|
|
5
|
-
type Priority = 'high' | 'low';
|
|
1
|
+
export type AnyUpdater = (...args: any[]) => any;
|
|
2
|
+
export type Listener = () => void;
|
|
6
3
|
interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
7
4
|
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
8
5
|
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
9
|
-
onUpdate?: (
|
|
10
|
-
priority: Priority;
|
|
11
|
-
}) => void;
|
|
12
|
-
defaultPriority?: Priority;
|
|
6
|
+
onUpdate?: () => void;
|
|
13
7
|
}
|
|
14
|
-
declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
8
|
+
export declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
15
9
|
listeners: Set<Listener>;
|
|
16
10
|
state: TState;
|
|
17
11
|
options?: StoreOptions<TState, TUpdater>;
|
|
18
12
|
_batching: boolean;
|
|
19
13
|
_flushing: number;
|
|
20
|
-
_nextPriority: null | Priority;
|
|
21
14
|
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
|
|
22
15
|
subscribe: (listener: Listener) => () => void;
|
|
23
|
-
setState: (updater: TUpdater
|
|
24
|
-
priority: Priority;
|
|
25
|
-
}) => void;
|
|
16
|
+
setState: (updater: TUpdater) => void;
|
|
26
17
|
_flush: () => void;
|
|
27
18
|
batch: (cb: () => void) => void;
|
|
28
19
|
}
|
|
29
|
-
|
|
30
|
-
export { AnyUpdater, Listener, Priority, Store };
|
|
20
|
+
export {};
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
var Store = class {
|
|
1
|
+
class Store {
|
|
3
2
|
constructor(initialState, options) {
|
|
4
3
|
this.listeners = /* @__PURE__ */ new Set();
|
|
5
4
|
this._batching = false;
|
|
6
5
|
this._flushing = 0;
|
|
7
|
-
this._nextPriority = null;
|
|
8
6
|
this.subscribe = (listener) => {
|
|
9
7
|
var _a, _b;
|
|
10
8
|
this.listeners.add(listener);
|
|
@@ -14,21 +12,11 @@ var Store = class {
|
|
|
14
12
|
unsub == null ? void 0 : unsub();
|
|
15
13
|
};
|
|
16
14
|
};
|
|
17
|
-
this.setState = (updater
|
|
18
|
-
var _a, _b, _c
|
|
15
|
+
this.setState = (updater) => {
|
|
16
|
+
var _a, _b, _c;
|
|
19
17
|
const previous = this.state;
|
|
20
18
|
this.state = ((_a = this.options) == null ? void 0 : _a.updateFn) ? this.options.updateFn(previous)(updater) : updater(previous);
|
|
21
|
-
|
|
22
|
-
if (this._nextPriority === null) {
|
|
23
|
-
this._nextPriority = priority;
|
|
24
|
-
} else if (this._nextPriority === "high") {
|
|
25
|
-
this._nextPriority = priority;
|
|
26
|
-
} else {
|
|
27
|
-
this._nextPriority = ((_c = this.options) == null ? void 0 : _c.defaultPriority) ?? "high";
|
|
28
|
-
}
|
|
29
|
-
(_e = (_d = this.options) == null ? void 0 : _d.onUpdate) == null ? void 0 : _e.call(_d, {
|
|
30
|
-
priority: this._nextPriority
|
|
31
|
-
});
|
|
19
|
+
(_c = (_b = this.options) == null ? void 0 : _b.onUpdate) == null ? void 0 : _c.call(_b);
|
|
32
20
|
this._flush();
|
|
33
21
|
};
|
|
34
22
|
this._flush = () => {
|
|
@@ -38,9 +26,7 @@ var Store = class {
|
|
|
38
26
|
this.listeners.forEach((listener) => {
|
|
39
27
|
if (this._flushing !== flushId)
|
|
40
28
|
return;
|
|
41
|
-
listener(
|
|
42
|
-
priority: this._nextPriority ?? "high"
|
|
43
|
-
});
|
|
29
|
+
listener();
|
|
44
30
|
});
|
|
45
31
|
};
|
|
46
32
|
this.batch = (cb) => {
|
|
@@ -54,8 +40,8 @@ var Store = class {
|
|
|
54
40
|
this.state = initialState;
|
|
55
41
|
this.options = options;
|
|
56
42
|
}
|
|
57
|
-
}
|
|
43
|
+
}
|
|
58
44
|
export {
|
|
59
45
|
Store
|
|
60
46
|
};
|
|
61
|
-
//# sourceMappingURL=index.js.map
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = () => void\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: () => void\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (updater: TUpdater) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.()\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener()\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"names":[],"mappings":"AAgBO,MAAM,MAGX;AAAA,EAOA,YAAY,cAAsB,SAA0C;AAN5E,SAAA,gCAAgB;AAGJ,SAAA,YAAA;AACA,SAAA,YAAA;AAOZ,SAAA,YAAY,CAAC,aAAuB;AAf/B;AAgBE,WAAA,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACN,aAAA,UAAU,OAAO,QAAQ;AACtB;AAAA,MAAA;AAAA,IACV;AAGF,SAAA,WAAW,CAAC,YAAsB;AAxB7B;AAyBH,YAAM,WAAW,KAAK;AACtB,WAAK,UAAQ,UAAK,YAAL,mBAAc,YACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAG7B,uBAAK,YAAL,mBAAc,aAAd;AAGA,WAAK,OAAO;AAAA,IAAA;AAGd,SAAA,SAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACd,YAAA,UAAU,EAAE,KAAK;AAClB,WAAA,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AACvB;MAAA,CACV;AAAA,IAAA;AAGH,SAAA,QAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACd;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IAAA;AAxCZ,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AAwCF;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/store",
|
|
3
3
|
"author": "Tanner Linsley",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.3.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "tanstack/store",
|
|
7
7
|
"homepage": "https://tanstack.com/store",
|
|
@@ -18,34 +18,38 @@
|
|
|
18
18
|
"url": "https://github.com/sponsors/tannerlinsley"
|
|
19
19
|
},
|
|
20
20
|
"type": "module",
|
|
21
|
-
"types": "
|
|
22
|
-
"main": "
|
|
23
|
-
"module": "
|
|
21
|
+
"types": "dist/esm/index.d.ts",
|
|
22
|
+
"main": "dist/cjs/index.cjs",
|
|
23
|
+
"module": "dist/esm/index.js",
|
|
24
24
|
"exports": {
|
|
25
25
|
".": {
|
|
26
26
|
"import": {
|
|
27
|
-
"types": "./
|
|
28
|
-
"default": "./
|
|
27
|
+
"types": "./dist/esm/index.d.ts",
|
|
28
|
+
"default": "./dist/esm/index.js"
|
|
29
29
|
},
|
|
30
30
|
"require": {
|
|
31
|
-
"types": "./
|
|
32
|
-
"default": "./
|
|
31
|
+
"types": "./dist/cjs/index.d.cts",
|
|
32
|
+
"default": "./dist/cjs/index.cjs"
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"./package.json": "./package.json"
|
|
36
36
|
},
|
|
37
37
|
"sideEffects": false,
|
|
38
38
|
"files": [
|
|
39
|
-
"
|
|
39
|
+
"dist",
|
|
40
40
|
"src"
|
|
41
41
|
],
|
|
42
42
|
"scripts": {
|
|
43
|
-
"clean": "rimraf ./
|
|
43
|
+
"clean": "rimraf ./dist && rimraf ./coverage",
|
|
44
44
|
"test:eslint": "eslint --ext .ts,.tsx ./src",
|
|
45
|
-
"test:types": "tsc",
|
|
46
|
-
"test:
|
|
45
|
+
"test:types:versions49": "node ../../node_modules/typescript49/lib/tsc.js --project tsconfig.legacy.json",
|
|
46
|
+
"test:types:versions50": "node ../../node_modules/typescript50/lib/tsc.js",
|
|
47
|
+
"test:types:versions51": "node ../../node_modules/typescript51/lib/tsc.js",
|
|
48
|
+
"test:types:versions52": "tsc",
|
|
49
|
+
"test:types": "pnpm run \"/^test:types:versions.*/\"",
|
|
50
|
+
"test:lib": "vitest",
|
|
47
51
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
48
52
|
"test:build": "publint --strict",
|
|
49
|
-
"build": "
|
|
53
|
+
"build": "vite build"
|
|
50
54
|
}
|
|
51
55
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
export type AnyUpdater = (...args: any[]) => any
|
|
2
2
|
|
|
3
|
-
export type Listener = (
|
|
4
|
-
|
|
5
|
-
export type Priority = 'high' | 'low'
|
|
3
|
+
export type Listener = () => void
|
|
6
4
|
|
|
7
5
|
interface StoreOptions<
|
|
8
6
|
TState,
|
|
@@ -13,8 +11,7 @@ interface StoreOptions<
|
|
|
13
11
|
listener: Listener,
|
|
14
12
|
store: Store<TState, TUpdater>,
|
|
15
13
|
) => () => void
|
|
16
|
-
onUpdate?: (
|
|
17
|
-
defaultPriority?: Priority
|
|
14
|
+
onUpdate?: () => void
|
|
18
15
|
}
|
|
19
16
|
|
|
20
17
|
export class Store<
|
|
@@ -26,7 +23,6 @@ export class Store<
|
|
|
26
23
|
options?: StoreOptions<TState, TUpdater>
|
|
27
24
|
_batching = false
|
|
28
25
|
_flushing = 0
|
|
29
|
-
_nextPriority: null | Priority = null
|
|
30
26
|
|
|
31
27
|
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {
|
|
32
28
|
this.state = initialState
|
|
@@ -42,30 +38,14 @@ export class Store<
|
|
|
42
38
|
}
|
|
43
39
|
}
|
|
44
40
|
|
|
45
|
-
setState = (
|
|
46
|
-
updater: TUpdater,
|
|
47
|
-
opts?: {
|
|
48
|
-
priority: Priority
|
|
49
|
-
},
|
|
50
|
-
) => {
|
|
41
|
+
setState = (updater: TUpdater) => {
|
|
51
42
|
const previous = this.state
|
|
52
43
|
this.state = this.options?.updateFn
|
|
53
44
|
? this.options.updateFn(previous)(updater)
|
|
54
45
|
: (updater as any)(previous)
|
|
55
46
|
|
|
56
|
-
const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'
|
|
57
|
-
if (this._nextPriority === null) {
|
|
58
|
-
this._nextPriority = priority
|
|
59
|
-
} else if (this._nextPriority === 'high') {
|
|
60
|
-
this._nextPriority = priority
|
|
61
|
-
} else {
|
|
62
|
-
this._nextPriority = this.options?.defaultPriority ?? 'high'
|
|
63
|
-
}
|
|
64
|
-
|
|
65
47
|
// Always run onUpdate, regardless of batching
|
|
66
|
-
this.options?.onUpdate?.(
|
|
67
|
-
priority: this._nextPriority,
|
|
68
|
-
})
|
|
48
|
+
this.options?.onUpdate?.()
|
|
69
49
|
|
|
70
50
|
// Attempt to flush
|
|
71
51
|
this._flush()
|
|
@@ -76,9 +56,7 @@ export class Store<
|
|
|
76
56
|
const flushId = ++this._flushing
|
|
77
57
|
this.listeners.forEach((listener) => {
|
|
78
58
|
if (this._flushing !== flushId) return
|
|
79
|
-
listener(
|
|
80
|
-
priority: this._nextPriority ?? 'high',
|
|
81
|
-
})
|
|
59
|
+
listener()
|
|
82
60
|
})
|
|
83
61
|
}
|
|
84
62
|
|
package/build/legacy/index.cjs
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
Store: () => Store
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
var Store = class {
|
|
27
|
-
constructor(initialState, options) {
|
|
28
|
-
this.listeners = /* @__PURE__ */ new Set();
|
|
29
|
-
this._batching = false;
|
|
30
|
-
this._flushing = 0;
|
|
31
|
-
this._nextPriority = null;
|
|
32
|
-
this.subscribe = (listener) => {
|
|
33
|
-
var _a, _b;
|
|
34
|
-
this.listeners.add(listener);
|
|
35
|
-
const unsub = (_b = (_a = this.options) == null ? void 0 : _a.onSubscribe) == null ? void 0 : _b.call(_a, listener, this);
|
|
36
|
-
return () => {
|
|
37
|
-
this.listeners.delete(listener);
|
|
38
|
-
unsub == null ? void 0 : unsub();
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
this.setState = (updater, opts) => {
|
|
42
|
-
var _a, _b, _c, _d, _e;
|
|
43
|
-
const previous = this.state;
|
|
44
|
-
this.state = ((_a = this.options) == null ? void 0 : _a.updateFn) ? this.options.updateFn(previous)(updater) : updater(previous);
|
|
45
|
-
const priority = (opts == null ? void 0 : opts.priority) ?? ((_b = this.options) == null ? void 0 : _b.defaultPriority) ?? "high";
|
|
46
|
-
if (this._nextPriority === null) {
|
|
47
|
-
this._nextPriority = priority;
|
|
48
|
-
} else if (this._nextPriority === "high") {
|
|
49
|
-
this._nextPriority = priority;
|
|
50
|
-
} else {
|
|
51
|
-
this._nextPriority = ((_c = this.options) == null ? void 0 : _c.defaultPriority) ?? "high";
|
|
52
|
-
}
|
|
53
|
-
(_e = (_d = this.options) == null ? void 0 : _d.onUpdate) == null ? void 0 : _e.call(_d, {
|
|
54
|
-
priority: this._nextPriority
|
|
55
|
-
});
|
|
56
|
-
this._flush();
|
|
57
|
-
};
|
|
58
|
-
this._flush = () => {
|
|
59
|
-
if (this._batching)
|
|
60
|
-
return;
|
|
61
|
-
const flushId = ++this._flushing;
|
|
62
|
-
this.listeners.forEach((listener) => {
|
|
63
|
-
if (this._flushing !== flushId)
|
|
64
|
-
return;
|
|
65
|
-
listener({
|
|
66
|
-
priority: this._nextPriority ?? "high"
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
this.batch = (cb) => {
|
|
71
|
-
if (this._batching)
|
|
72
|
-
return cb();
|
|
73
|
-
this._batching = true;
|
|
74
|
-
cb();
|
|
75
|
-
this._batching = false;
|
|
76
|
-
this._flush();
|
|
77
|
-
};
|
|
78
|
-
this.state = initialState;
|
|
79
|
-
this.options = options;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
-
0 && (module.exports = {
|
|
84
|
-
Store
|
|
85
|
-
});
|
|
86
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = (opts: { priority: Priority }) => void\n\nexport type Priority = 'high' | 'low'\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: (opts: { priority: Priority }) => void\n defaultPriority?: Priority\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n _nextPriority: null | Priority = null\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (\n updater: TUpdater,\n opts?: {\n priority: Priority\n },\n ) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'\n if (this._nextPriority === null) {\n this._nextPriority = priority\n } else if (this._nextPriority === 'high') {\n this._nextPriority = priority\n } else {\n this._nextPriority = this.options?.defaultPriority ?? 'high'\n }\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.({\n priority: this._nextPriority,\n })\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener({\n priority: this._nextPriority ?? 'high',\n })\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBO,IAAM,QAAN,MAGL;AAAA,EAQA,YAAY,cAAsB,SAA0C;AAP5E,qBAAY,oBAAI,IAAc;AAG9B,qBAAY;AACZ,qBAAY;AACZ,yBAAiC;AAOjC,qBAAY,CAAC,aAAuB;AAnCtC;AAoCI,WAAK,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACX,aAAK,UAAU,OAAO,QAAQ;AAC9B;AAAA,MACF;AAAA,IACF;AAEA,oBAAW,CACT,SACA,SAGG;AAjDP;AAkDI,YAAM,WAAW,KAAK;AACtB,WAAK,UAAQ,UAAK,YAAL,mBAAc,YACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAE7B,YAAM,YAAW,6BAAM,eAAY,UAAK,YAAL,mBAAc,oBAAmB;AACpE,UAAI,KAAK,kBAAkB,MAAM;AAC/B,aAAK,gBAAgB;AAAA,MACvB,WAAW,KAAK,kBAAkB,QAAQ;AACxC,aAAK,gBAAgB;AAAA,MACvB,OAAO;AACL,aAAK,kBAAgB,UAAK,YAAL,mBAAc,oBAAmB;AAAA,MACxD;AAGA,uBAAK,YAAL,mBAAc,aAAd,4BAAyB;AAAA,QACvB,UAAU,KAAK;AAAA,MACjB;AAGA,WAAK,OAAO;AAAA,IACd;AAEA,kBAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACpB,YAAM,UAAU,EAAE,KAAK;AACvB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AAChC,iBAAS;AAAA,UACP,UAAU,KAAK,iBAAiB;AAAA,QAClC,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,iBAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACjB,SAAG;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA3DE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AA0DF;","names":[]}
|
package/build/legacy/index.d.cts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
type AnyUpdater = (...args: any[]) => any;
|
|
2
|
-
type Listener = (opts: {
|
|
3
|
-
priority: Priority;
|
|
4
|
-
}) => void;
|
|
5
|
-
type Priority = 'high' | 'low';
|
|
6
|
-
interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
7
|
-
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
8
|
-
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
9
|
-
onUpdate?: (opts: {
|
|
10
|
-
priority: Priority;
|
|
11
|
-
}) => void;
|
|
12
|
-
defaultPriority?: Priority;
|
|
13
|
-
}
|
|
14
|
-
declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
15
|
-
listeners: Set<Listener>;
|
|
16
|
-
state: TState;
|
|
17
|
-
options?: StoreOptions<TState, TUpdater>;
|
|
18
|
-
_batching: boolean;
|
|
19
|
-
_flushing: number;
|
|
20
|
-
_nextPriority: null | Priority;
|
|
21
|
-
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
|
|
22
|
-
subscribe: (listener: Listener) => () => void;
|
|
23
|
-
setState: (updater: TUpdater, opts?: {
|
|
24
|
-
priority: Priority;
|
|
25
|
-
}) => void;
|
|
26
|
-
_flush: () => void;
|
|
27
|
-
batch: (cb: () => void) => void;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export { AnyUpdater, Listener, Priority, Store };
|
package/build/legacy/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
type AnyUpdater = (...args: any[]) => any;
|
|
2
|
-
type Listener = (opts: {
|
|
3
|
-
priority: Priority;
|
|
4
|
-
}) => void;
|
|
5
|
-
type Priority = 'high' | 'low';
|
|
6
|
-
interface StoreOptions<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
7
|
-
updateFn?: (previous: TState) => (updater: TUpdater) => TState;
|
|
8
|
-
onSubscribe?: (listener: Listener, store: Store<TState, TUpdater>) => () => void;
|
|
9
|
-
onUpdate?: (opts: {
|
|
10
|
-
priority: Priority;
|
|
11
|
-
}) => void;
|
|
12
|
-
defaultPriority?: Priority;
|
|
13
|
-
}
|
|
14
|
-
declare class Store<TState, TUpdater extends AnyUpdater = (cb: TState) => TState> {
|
|
15
|
-
listeners: Set<Listener>;
|
|
16
|
-
state: TState;
|
|
17
|
-
options?: StoreOptions<TState, TUpdater>;
|
|
18
|
-
_batching: boolean;
|
|
19
|
-
_flushing: number;
|
|
20
|
-
_nextPriority: null | Priority;
|
|
21
|
-
constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>);
|
|
22
|
-
subscribe: (listener: Listener) => () => void;
|
|
23
|
-
setState: (updater: TUpdater, opts?: {
|
|
24
|
-
priority: Priority;
|
|
25
|
-
}) => void;
|
|
26
|
-
_flush: () => void;
|
|
27
|
-
batch: (cb: () => void) => void;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export { AnyUpdater, Listener, Priority, Store };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = (opts: { priority: Priority }) => void\n\nexport type Priority = 'high' | 'low'\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: (opts: { priority: Priority }) => void\n defaultPriority?: Priority\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n _nextPriority: null | Priority = null\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (\n updater: TUpdater,\n opts?: {\n priority: Priority\n },\n ) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'\n if (this._nextPriority === null) {\n this._nextPriority = priority\n } else if (this._nextPriority === 'high') {\n this._nextPriority = priority\n } else {\n this._nextPriority = this.options?.defaultPriority ?? 'high'\n }\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.({\n priority: this._nextPriority,\n })\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener({\n priority: this._nextPriority ?? 'high',\n })\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"mappings":";AAmBO,IAAM,QAAN,MAGL;AAAA,EAQA,YAAY,cAAsB,SAA0C;AAP5E,qBAAY,oBAAI,IAAc;AAG9B,qBAAY;AACZ,qBAAY;AACZ,yBAAiC;AAOjC,qBAAY,CAAC,aAAuB;AAnCtC;AAoCI,WAAK,UAAU,IAAI,QAAQ;AAC3B,YAAM,SAAQ,gBAAK,YAAL,mBAAc,gBAAd,4BAA4B,UAAU;AACpD,aAAO,MAAM;AACX,aAAK,UAAU,OAAO,QAAQ;AAC9B;AAAA,MACF;AAAA,IACF;AAEA,oBAAW,CACT,SACA,SAGG;AAjDP;AAkDI,YAAM,WAAW,KAAK;AACtB,WAAK,UAAQ,UAAK,YAAL,mBAAc,YACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAE7B,YAAM,YAAW,6BAAM,eAAY,UAAK,YAAL,mBAAc,oBAAmB;AACpE,UAAI,KAAK,kBAAkB,MAAM;AAC/B,aAAK,gBAAgB;AAAA,MACvB,WAAW,KAAK,kBAAkB,QAAQ;AACxC,aAAK,gBAAgB;AAAA,MACvB,OAAO;AACL,aAAK,kBAAgB,UAAK,YAAL,mBAAc,oBAAmB;AAAA,MACxD;AAGA,uBAAK,YAAL,mBAAc,aAAd,4BAAyB;AAAA,QACvB,UAAU,KAAK;AAAA,MACjB;AAGA,WAAK,OAAO;AAAA,IACd;AAEA,kBAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACpB,YAAM,UAAU,EAAE,KAAK;AACvB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AAChC,iBAAS;AAAA,UACP,UAAU,KAAK,iBAAiB;AAAA,QAClC,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,iBAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACjB,SAAG;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA3DE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AA0DF;","names":[]}
|
package/build/modern/index.cjs
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
Store: () => Store
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
var Store = class {
|
|
27
|
-
constructor(initialState, options) {
|
|
28
|
-
this.listeners = /* @__PURE__ */ new Set();
|
|
29
|
-
this._batching = false;
|
|
30
|
-
this._flushing = 0;
|
|
31
|
-
this._nextPriority = null;
|
|
32
|
-
this.subscribe = (listener) => {
|
|
33
|
-
this.listeners.add(listener);
|
|
34
|
-
const unsub = this.options?.onSubscribe?.(listener, this);
|
|
35
|
-
return () => {
|
|
36
|
-
this.listeners.delete(listener);
|
|
37
|
-
unsub?.();
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
this.setState = (updater, opts) => {
|
|
41
|
-
const previous = this.state;
|
|
42
|
-
this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
|
|
43
|
-
const priority = opts?.priority ?? this.options?.defaultPriority ?? "high";
|
|
44
|
-
if (this._nextPriority === null) {
|
|
45
|
-
this._nextPriority = priority;
|
|
46
|
-
} else if (this._nextPriority === "high") {
|
|
47
|
-
this._nextPriority = priority;
|
|
48
|
-
} else {
|
|
49
|
-
this._nextPriority = this.options?.defaultPriority ?? "high";
|
|
50
|
-
}
|
|
51
|
-
this.options?.onUpdate?.({
|
|
52
|
-
priority: this._nextPriority
|
|
53
|
-
});
|
|
54
|
-
this._flush();
|
|
55
|
-
};
|
|
56
|
-
this._flush = () => {
|
|
57
|
-
if (this._batching)
|
|
58
|
-
return;
|
|
59
|
-
const flushId = ++this._flushing;
|
|
60
|
-
this.listeners.forEach((listener) => {
|
|
61
|
-
if (this._flushing !== flushId)
|
|
62
|
-
return;
|
|
63
|
-
listener({
|
|
64
|
-
priority: this._nextPriority ?? "high"
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
};
|
|
68
|
-
this.batch = (cb) => {
|
|
69
|
-
if (this._batching)
|
|
70
|
-
return cb();
|
|
71
|
-
this._batching = true;
|
|
72
|
-
cb();
|
|
73
|
-
this._batching = false;
|
|
74
|
-
this._flush();
|
|
75
|
-
};
|
|
76
|
-
this.state = initialState;
|
|
77
|
-
this.options = options;
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
-
0 && (module.exports = {
|
|
82
|
-
Store
|
|
83
|
-
});
|
|
84
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = (opts: { priority: Priority }) => void\n\nexport type Priority = 'high' | 'low'\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: (opts: { priority: Priority }) => void\n defaultPriority?: Priority\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n _nextPriority: null | Priority = null\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (\n updater: TUpdater,\n opts?: {\n priority: Priority\n },\n ) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'\n if (this._nextPriority === null) {\n this._nextPriority = priority\n } else if (this._nextPriority === 'high') {\n this._nextPriority = priority\n } else {\n this._nextPriority = this.options?.defaultPriority ?? 'high'\n }\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.({\n priority: this._nextPriority,\n })\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener({\n priority: this._nextPriority ?? 'high',\n })\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBO,IAAM,QAAN,MAGL;AAAA,EAQA,YAAY,cAAsB,SAA0C;AAP5E,qBAAY,oBAAI,IAAc;AAG9B,qBAAY;AACZ,qBAAY;AACZ,yBAAiC;AAOjC,qBAAY,CAAC,aAAuB;AAClC,WAAK,UAAU,IAAI,QAAQ;AAC3B,YAAM,QAAQ,KAAK,SAAS,cAAc,UAAU,IAAI;AACxD,aAAO,MAAM;AACX,aAAK,UAAU,OAAO,QAAQ;AAC9B,gBAAQ;AAAA,MACV;AAAA,IACF;AAEA,oBAAW,CACT,SACA,SAGG;AACH,YAAM,WAAW,KAAK;AACtB,WAAK,QAAQ,KAAK,SAAS,WACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAE7B,YAAM,WAAW,MAAM,YAAY,KAAK,SAAS,mBAAmB;AACpE,UAAI,KAAK,kBAAkB,MAAM;AAC/B,aAAK,gBAAgB;AAAA,MACvB,WAAW,KAAK,kBAAkB,QAAQ;AACxC,aAAK,gBAAgB;AAAA,MACvB,OAAO;AACL,aAAK,gBAAgB,KAAK,SAAS,mBAAmB;AAAA,MACxD;AAGA,WAAK,SAAS,WAAW;AAAA,QACvB,UAAU,KAAK;AAAA,MACjB,CAAC;AAGD,WAAK,OAAO;AAAA,IACd;AAEA,kBAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACpB,YAAM,UAAU,EAAE,KAAK;AACvB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AAChC,iBAAS;AAAA,UACP,UAAU,KAAK,iBAAiB;AAAA,QAClC,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,iBAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACjB,SAAG;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA3DE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AA0DF;","names":[]}
|
package/build/modern/index.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
var Store = class {
|
|
3
|
-
constructor(initialState, options) {
|
|
4
|
-
this.listeners = /* @__PURE__ */ new Set();
|
|
5
|
-
this._batching = false;
|
|
6
|
-
this._flushing = 0;
|
|
7
|
-
this._nextPriority = null;
|
|
8
|
-
this.subscribe = (listener) => {
|
|
9
|
-
this.listeners.add(listener);
|
|
10
|
-
const unsub = this.options?.onSubscribe?.(listener, this);
|
|
11
|
-
return () => {
|
|
12
|
-
this.listeners.delete(listener);
|
|
13
|
-
unsub?.();
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
this.setState = (updater, opts) => {
|
|
17
|
-
const previous = this.state;
|
|
18
|
-
this.state = this.options?.updateFn ? this.options.updateFn(previous)(updater) : updater(previous);
|
|
19
|
-
const priority = opts?.priority ?? this.options?.defaultPriority ?? "high";
|
|
20
|
-
if (this._nextPriority === null) {
|
|
21
|
-
this._nextPriority = priority;
|
|
22
|
-
} else if (this._nextPriority === "high") {
|
|
23
|
-
this._nextPriority = priority;
|
|
24
|
-
} else {
|
|
25
|
-
this._nextPriority = this.options?.defaultPriority ?? "high";
|
|
26
|
-
}
|
|
27
|
-
this.options?.onUpdate?.({
|
|
28
|
-
priority: this._nextPriority
|
|
29
|
-
});
|
|
30
|
-
this._flush();
|
|
31
|
-
};
|
|
32
|
-
this._flush = () => {
|
|
33
|
-
if (this._batching)
|
|
34
|
-
return;
|
|
35
|
-
const flushId = ++this._flushing;
|
|
36
|
-
this.listeners.forEach((listener) => {
|
|
37
|
-
if (this._flushing !== flushId)
|
|
38
|
-
return;
|
|
39
|
-
listener({
|
|
40
|
-
priority: this._nextPriority ?? "high"
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
this.batch = (cb) => {
|
|
45
|
-
if (this._batching)
|
|
46
|
-
return cb();
|
|
47
|
-
this._batching = true;
|
|
48
|
-
cb();
|
|
49
|
-
this._batching = false;
|
|
50
|
-
this._flush();
|
|
51
|
-
};
|
|
52
|
-
this.state = initialState;
|
|
53
|
-
this.options = options;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
export {
|
|
57
|
-
Store
|
|
58
|
-
};
|
|
59
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type AnyUpdater = (...args: any[]) => any\n\nexport type Listener = (opts: { priority: Priority }) => void\n\nexport type Priority = 'high' | 'low'\n\ninterface StoreOptions<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n updateFn?: (previous: TState) => (updater: TUpdater) => TState\n onSubscribe?: (\n listener: Listener,\n store: Store<TState, TUpdater>,\n ) => () => void\n onUpdate?: (opts: { priority: Priority }) => void\n defaultPriority?: Priority\n}\n\nexport class Store<\n TState,\n TUpdater extends AnyUpdater = (cb: TState) => TState,\n> {\n listeners = new Set<Listener>()\n state: TState\n options?: StoreOptions<TState, TUpdater>\n _batching = false\n _flushing = 0\n _nextPriority: null | Priority = null\n\n constructor(initialState: TState, options?: StoreOptions<TState, TUpdater>) {\n this.state = initialState\n this.options = options\n }\n\n subscribe = (listener: Listener) => {\n this.listeners.add(listener)\n const unsub = this.options?.onSubscribe?.(listener, this)\n return () => {\n this.listeners.delete(listener)\n unsub?.()\n }\n }\n\n setState = (\n updater: TUpdater,\n opts?: {\n priority: Priority\n },\n ) => {\n const previous = this.state\n this.state = this.options?.updateFn\n ? this.options.updateFn(previous)(updater)\n : (updater as any)(previous)\n\n const priority = opts?.priority ?? this.options?.defaultPriority ?? 'high'\n if (this._nextPriority === null) {\n this._nextPriority = priority\n } else if (this._nextPriority === 'high') {\n this._nextPriority = priority\n } else {\n this._nextPriority = this.options?.defaultPriority ?? 'high'\n }\n\n // Always run onUpdate, regardless of batching\n this.options?.onUpdate?.({\n priority: this._nextPriority,\n })\n\n // Attempt to flush\n this._flush()\n }\n\n _flush = () => {\n if (this._batching) return\n const flushId = ++this._flushing\n this.listeners.forEach((listener) => {\n if (this._flushing !== flushId) return\n listener({\n priority: this._nextPriority ?? 'high',\n })\n })\n }\n\n batch = (cb: () => void) => {\n if (this._batching) return cb()\n this._batching = true\n cb()\n this._batching = false\n this._flush()\n }\n}\n"],"mappings":";AAmBO,IAAM,QAAN,MAGL;AAAA,EAQA,YAAY,cAAsB,SAA0C;AAP5E,qBAAY,oBAAI,IAAc;AAG9B,qBAAY;AACZ,qBAAY;AACZ,yBAAiC;AAOjC,qBAAY,CAAC,aAAuB;AAClC,WAAK,UAAU,IAAI,QAAQ;AAC3B,YAAM,QAAQ,KAAK,SAAS,cAAc,UAAU,IAAI;AACxD,aAAO,MAAM;AACX,aAAK,UAAU,OAAO,QAAQ;AAC9B,gBAAQ;AAAA,MACV;AAAA,IACF;AAEA,oBAAW,CACT,SACA,SAGG;AACH,YAAM,WAAW,KAAK;AACtB,WAAK,QAAQ,KAAK,SAAS,WACvB,KAAK,QAAQ,SAAS,QAAQ,EAAE,OAAO,IACtC,QAAgB,QAAQ;AAE7B,YAAM,WAAW,MAAM,YAAY,KAAK,SAAS,mBAAmB;AACpE,UAAI,KAAK,kBAAkB,MAAM;AAC/B,aAAK,gBAAgB;AAAA,MACvB,WAAW,KAAK,kBAAkB,QAAQ;AACxC,aAAK,gBAAgB;AAAA,MACvB,OAAO;AACL,aAAK,gBAAgB,KAAK,SAAS,mBAAmB;AAAA,MACxD;AAGA,WAAK,SAAS,WAAW;AAAA,QACvB,UAAU,KAAK;AAAA,MACjB,CAAC;AAGD,WAAK,OAAO;AAAA,IACd;AAEA,kBAAS,MAAM;AACb,UAAI,KAAK;AAAW;AACpB,YAAM,UAAU,EAAE,KAAK;AACvB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,YAAI,KAAK,cAAc;AAAS;AAChC,iBAAS;AAAA,UACP,UAAU,KAAK,iBAAiB;AAAA,QAClC,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,iBAAQ,CAAC,OAAmB;AAC1B,UAAI,KAAK;AAAW,eAAO,GAAG;AAC9B,WAAK,YAAY;AACjB,SAAG;AACH,WAAK,YAAY;AACjB,WAAK,OAAO;AAAA,IACd;AA3DE,SAAK,QAAQ;AACb,SAAK,UAAU;AAAA,EACjB;AA0DF;","names":[]}
|
|
File without changes
|