@webneat/loop 0.0.3
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/LICENSE +21 -0
- package/dist/index.cjs +90 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +67 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Amine Ben hammou
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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 index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
done: () => done,
|
|
24
|
+
loop: () => loop,
|
|
25
|
+
stores: () => stores_exports
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/done.ts
|
|
30
|
+
var DONE = /* @__PURE__ */ Symbol("loop:done");
|
|
31
|
+
function done() {
|
|
32
|
+
return DONE;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/loop.ts
|
|
36
|
+
function loop(act) {
|
|
37
|
+
return async (initial, store) => {
|
|
38
|
+
let state = store ? await store.load() : void 0;
|
|
39
|
+
if (state === void 0) state = initial;
|
|
40
|
+
while (true) {
|
|
41
|
+
const next_state = await act(state);
|
|
42
|
+
if (next_state === DONE) return state;
|
|
43
|
+
state = next_state;
|
|
44
|
+
if (store) await store.save(state);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/stores.ts
|
|
50
|
+
var stores_exports = {};
|
|
51
|
+
__export(stores_exports, {
|
|
52
|
+
json: () => json,
|
|
53
|
+
memory: () => memory
|
|
54
|
+
});
|
|
55
|
+
var import_promises = require("fs/promises");
|
|
56
|
+
function memory() {
|
|
57
|
+
let state;
|
|
58
|
+
return {
|
|
59
|
+
load: async () => state,
|
|
60
|
+
save: async (next_state) => {
|
|
61
|
+
state = next_state;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function json(path) {
|
|
66
|
+
return {
|
|
67
|
+
load: async () => {
|
|
68
|
+
if (!await file_exists(path)) return void 0;
|
|
69
|
+
return JSON.parse(await (0, import_promises.readFile)(path, "utf8"));
|
|
70
|
+
},
|
|
71
|
+
save: async (state) => {
|
|
72
|
+
await (0, import_promises.writeFile)(path, JSON.stringify(state), "utf8");
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
async function file_exists(filename) {
|
|
77
|
+
try {
|
|
78
|
+
await (0, import_promises.access)(filename);
|
|
79
|
+
return true;
|
|
80
|
+
} catch {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
85
|
+
0 && (module.exports = {
|
|
86
|
+
done,
|
|
87
|
+
loop,
|
|
88
|
+
stores
|
|
89
|
+
});
|
|
90
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/done.ts","../src/loop.ts","../src/stores.ts"],"sourcesContent":["export type { Store } from './types'\nexport { done } from './done'\nexport { loop } from './loop'\nexport * as stores from './stores'\n","export const DONE = Symbol('loop:done')\n\nexport function done(): typeof DONE {\n return DONE\n}\n","import { DONE } from './done'\nimport type { ActFn, RunFn } from './types'\n\nexport function loop<State, const NextState extends State = State>(act: ActFn<State, NextState>): RunFn<State> {\n return async (initial, store) => {\n let state: State | undefined = store ? await store.load() : undefined\n if (state === undefined) state = initial\n\n while (true) {\n const next_state = await act(state)\n if (next_state === DONE) return state\n\n state = next_state\n if (store) await store.save(state)\n }\n }\n}\n","import { readFile, writeFile, access } from 'fs/promises'\nimport type { Store } from './types'\n\nexport function memory<State>(): Store<State> {\n let state: State | undefined\n return {\n load: async () => state,\n save: async (next_state) => {\n state = next_state\n },\n }\n}\n\nexport function json<State>(path: string): Store<State> {\n return {\n load: async () => {\n if (! await file_exists(path)) return undefined\n return JSON.parse(await readFile(path, 'utf8'))\n },\n save: async (state) => {\n await writeFile(path, JSON.stringify(state), 'utf8')\n },\n }\n}\n\nasync function file_exists(filename: string) {\n try {\n await access(filename)\n return true\n } catch {\n return false\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,OAAO,uBAAO,WAAW;AAE/B,SAAS,OAAoB;AAClC,SAAO;AACT;;;ACDO,SAAS,KAAmD,KAA4C;AAC7G,SAAO,OAAO,SAAS,UAAU;AAC/B,QAAI,QAA2B,QAAQ,MAAM,MAAM,KAAK,IAAI;AAC5D,QAAI,UAAU,OAAW,SAAQ;AAEjC,WAAO,MAAM;AACX,YAAM,aAAa,MAAM,IAAI,KAAK;AAClC,UAAI,eAAe,KAAM,QAAO;AAEhC,cAAQ;AACR,UAAI,MAAO,OAAM,MAAM,KAAK,KAAK;AAAA,IACnC;AAAA,EACF;AACF;;;AChBA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAA4C;AAGrC,SAAS,SAA8B;AAC5C,MAAI;AACJ,SAAO;AAAA,IACL,MAAM,YAAY;AAAA,IAClB,MAAM,OAAO,eAAe;AAC1B,cAAQ;AAAA,IACV;AAAA,EACF;AACF;AAEO,SAAS,KAAY,MAA4B;AACtD,SAAO;AAAA,IACL,MAAM,YAAY;AAChB,UAAI,CAAE,MAAM,YAAY,IAAI,EAAG,QAAO;AACtC,aAAO,KAAK,MAAM,UAAM,0BAAS,MAAM,MAAM,CAAC;AAAA,IAChD;AAAA,IACA,MAAM,OAAO,UAAU;AACrB,gBAAM,2BAAU,MAAM,KAAK,UAAU,KAAK,GAAG,MAAM;AAAA,IACrD;AAAA,EACF;AACF;AAEA,eAAe,YAAY,UAAkB;AAC3C,MAAI;AACF,cAAM,wBAAO,QAAQ;AACrB,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare const DONE: unique symbol;
|
|
2
|
+
declare function done(): typeof DONE;
|
|
3
|
+
|
|
4
|
+
interface Store<State> {
|
|
5
|
+
load(): Promise<State | undefined>;
|
|
6
|
+
save(state: State): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
type ActFn<State, NextState extends State = State> = (state: State) => Promise<NextState | ReturnType<typeof done>>;
|
|
9
|
+
type RunFn<State> = (initial: State, store?: Store<State>) => Promise<State>;
|
|
10
|
+
|
|
11
|
+
declare function loop<State, const NextState extends State = State>(act: ActFn<State, NextState>): RunFn<State>;
|
|
12
|
+
|
|
13
|
+
declare function memory<State>(): Store<State>;
|
|
14
|
+
declare function json<State>(path: string): Store<State>;
|
|
15
|
+
|
|
16
|
+
declare const stores_json: typeof json;
|
|
17
|
+
declare const stores_memory: typeof memory;
|
|
18
|
+
declare namespace stores {
|
|
19
|
+
export { stores_json as json, stores_memory as memory };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { type Store, done, loop, stores };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare const DONE: unique symbol;
|
|
2
|
+
declare function done(): typeof DONE;
|
|
3
|
+
|
|
4
|
+
interface Store<State> {
|
|
5
|
+
load(): Promise<State | undefined>;
|
|
6
|
+
save(state: State): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
type ActFn<State, NextState extends State = State> = (state: State) => Promise<NextState | ReturnType<typeof done>>;
|
|
9
|
+
type RunFn<State> = (initial: State, store?: Store<State>) => Promise<State>;
|
|
10
|
+
|
|
11
|
+
declare function loop<State, const NextState extends State = State>(act: ActFn<State, NextState>): RunFn<State>;
|
|
12
|
+
|
|
13
|
+
declare function memory<State>(): Store<State>;
|
|
14
|
+
declare function json<State>(path: string): Store<State>;
|
|
15
|
+
|
|
16
|
+
declare const stores_json: typeof json;
|
|
17
|
+
declare const stores_memory: typeof memory;
|
|
18
|
+
declare namespace stores {
|
|
19
|
+
export { stores_json as json, stores_memory as memory };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { type Store, done, loop, stores };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/done.ts
|
|
8
|
+
var DONE = /* @__PURE__ */ Symbol("loop:done");
|
|
9
|
+
function done() {
|
|
10
|
+
return DONE;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/loop.ts
|
|
14
|
+
function loop(act) {
|
|
15
|
+
return async (initial, store) => {
|
|
16
|
+
let state = store ? await store.load() : void 0;
|
|
17
|
+
if (state === void 0) state = initial;
|
|
18
|
+
while (true) {
|
|
19
|
+
const next_state = await act(state);
|
|
20
|
+
if (next_state === DONE) return state;
|
|
21
|
+
state = next_state;
|
|
22
|
+
if (store) await store.save(state);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/stores.ts
|
|
28
|
+
var stores_exports = {};
|
|
29
|
+
__export(stores_exports, {
|
|
30
|
+
json: () => json,
|
|
31
|
+
memory: () => memory
|
|
32
|
+
});
|
|
33
|
+
import { readFile, writeFile, access } from "fs/promises";
|
|
34
|
+
function memory() {
|
|
35
|
+
let state;
|
|
36
|
+
return {
|
|
37
|
+
load: async () => state,
|
|
38
|
+
save: async (next_state) => {
|
|
39
|
+
state = next_state;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function json(path) {
|
|
44
|
+
return {
|
|
45
|
+
load: async () => {
|
|
46
|
+
if (!await file_exists(path)) return void 0;
|
|
47
|
+
return JSON.parse(await readFile(path, "utf8"));
|
|
48
|
+
},
|
|
49
|
+
save: async (state) => {
|
|
50
|
+
await writeFile(path, JSON.stringify(state), "utf8");
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
async function file_exists(filename) {
|
|
55
|
+
try {
|
|
56
|
+
await access(filename);
|
|
57
|
+
return true;
|
|
58
|
+
} catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export {
|
|
63
|
+
done,
|
|
64
|
+
loop,
|
|
65
|
+
stores_exports as stores
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/done.ts","../src/loop.ts","../src/stores.ts"],"sourcesContent":["export const DONE = Symbol('loop:done')\n\nexport function done(): typeof DONE {\n return DONE\n}\n","import { DONE } from './done'\nimport type { ActFn, RunFn } from './types'\n\nexport function loop<State, const NextState extends State = State>(act: ActFn<State, NextState>): RunFn<State> {\n return async (initial, store) => {\n let state: State | undefined = store ? await store.load() : undefined\n if (state === undefined) state = initial\n\n while (true) {\n const next_state = await act(state)\n if (next_state === DONE) return state\n\n state = next_state\n if (store) await store.save(state)\n }\n }\n}\n","import { readFile, writeFile, access } from 'fs/promises'\nimport type { Store } from './types'\n\nexport function memory<State>(): Store<State> {\n let state: State | undefined\n return {\n load: async () => state,\n save: async (next_state) => {\n state = next_state\n },\n }\n}\n\nexport function json<State>(path: string): Store<State> {\n return {\n load: async () => {\n if (! await file_exists(path)) return undefined\n return JSON.parse(await readFile(path, 'utf8'))\n },\n save: async (state) => {\n await writeFile(path, JSON.stringify(state), 'utf8')\n },\n }\n}\n\nasync function file_exists(filename: string) {\n try {\n await access(filename)\n return true\n } catch {\n return false\n }\n}\n"],"mappings":";;;;;;;AAAO,IAAM,OAAO,uBAAO,WAAW;AAE/B,SAAS,OAAoB;AAClC,SAAO;AACT;;;ACDO,SAAS,KAAmD,KAA4C;AAC7G,SAAO,OAAO,SAAS,UAAU;AAC/B,QAAI,QAA2B,QAAQ,MAAM,MAAM,KAAK,IAAI;AAC5D,QAAI,UAAU,OAAW,SAAQ;AAEjC,WAAO,MAAM;AACX,YAAM,aAAa,MAAM,IAAI,KAAK;AAClC,UAAI,eAAe,KAAM,QAAO;AAEhC,cAAQ;AACR,UAAI,MAAO,OAAM,MAAM,KAAK,KAAK;AAAA,IACnC;AAAA,EACF;AACF;;;AChBA;AAAA;AAAA;AAAA;AAAA;AAAA,SAAS,UAAU,WAAW,cAAc;AAGrC,SAAS,SAA8B;AAC5C,MAAI;AACJ,SAAO;AAAA,IACL,MAAM,YAAY;AAAA,IAClB,MAAM,OAAO,eAAe;AAC1B,cAAQ;AAAA,IACV;AAAA,EACF;AACF;AAEO,SAAS,KAAY,MAA4B;AACtD,SAAO;AAAA,IACL,MAAM,YAAY;AAChB,UAAI,CAAE,MAAM,YAAY,IAAI,EAAG,QAAO;AACtC,aAAO,KAAK,MAAM,MAAM,SAAS,MAAM,MAAM,CAAC;AAAA,IAChD;AAAA,IACA,MAAM,OAAO,UAAU;AACrB,YAAM,UAAU,MAAM,KAAK,UAAU,KAAK,GAAG,MAAM;AAAA,IACrD;AAAA,EACF;AACF;AAEA,eAAe,YAAY,UAAkB;AAC3C,MAAI;AACF,UAAM,OAAO,QAAQ;AACrB,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webneat/loop",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "Resumable loops with persisted state",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Amine Ben hammou",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"volta": {
|
|
23
|
+
"node": "26.8.1"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsup",
|
|
27
|
+
"test": "vitest run",
|
|
28
|
+
"test-watch": "vitest",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"format": "prettier --write ."
|
|
31
|
+
}
|
|
32
|
+
}
|