@milkdown/ctx
Advanced tools
Comparing version 6.4.1 to 6.5.0
import { $Slice, Slice } from './slice'; | ||
export declare type Container = { | ||
readonly sliceMap: Map<symbol, $Slice>; | ||
readonly getSlice: <T, N extends string = string>(slice: Slice<T, N> | N) => $Slice<T, N>; | ||
readonly sliceMap: Map<symbol, $Slice>; | ||
readonly removeSlice: <T, N extends string = string>(slice: Slice<T, N> | N) => void; | ||
}; | ||
export declare const createContainer: () => Container; | ||
//# sourceMappingURL=container.d.ts.map |
@@ -1,109 +0,125 @@ | ||
var S = (e, s, i) => { | ||
if (!s.has(e)) | ||
throw TypeError("Cannot " + i); | ||
var T = (e, i, r) => { | ||
if (!i.has(e)) | ||
throw TypeError("Cannot " + r); | ||
}; | ||
var o = (e, s, i) => (S(e, s, "read from private field"), i ? i.call(e) : s.get(e)), h = (e, s, i) => { | ||
if (s.has(e)) | ||
var n = (e, i, r) => (T(e, i, "read from private field"), r ? r.call(e) : i.get(e)), h = (e, i, r) => { | ||
if (i.has(e)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
s instanceof WeakSet ? s.add(e) : s.set(e, i); | ||
}, a = (e, s, i, t) => (S(e, s, "write to private field"), t ? t.call(e, i) : s.set(e, i), i); | ||
import { contextNotFound as E, ctxCallOutOfScope as T, timerNotFound as M } from "@milkdown/exception"; | ||
const N = () => { | ||
i instanceof WeakSet ? i.add(e) : i.set(e, r); | ||
}, a = (e, i, r, t) => (T(e, i, "write to private field"), t ? t.call(e, r) : i.set(e, r), r); | ||
import { contextNotFound as N, ctxCallOutOfScope as P, timerNotFound as k } from "@milkdown/exception"; | ||
const j = () => { | ||
const e = /* @__PURE__ */ new Map(); | ||
return { getSlice: (i) => { | ||
const t = typeof i == "string" ? [...e.values()].find((n) => n.name === i) : e.get(i.id); | ||
if (!t) { | ||
const n = typeof i == "string" ? i : i.sliceName; | ||
throw E(n); | ||
return { sliceMap: e, getSlice: (t) => { | ||
const s = typeof t == "string" ? [...e.values()].find((o) => o.name === t) : e.get(t.id); | ||
if (!s) { | ||
const o = typeof t == "string" ? t : t.sliceName; | ||
throw N(o); | ||
} | ||
return t; | ||
}, sliceMap: e }; | ||
}, P = (e) => Array.isArray(e) ? [...e] : typeof e == "object" ? { ...e } : e, j = (e, s) => { | ||
const i = Symbol("Context"), t = (n, d = P(e)) => { | ||
let r = d; | ||
const m = { | ||
name: s, | ||
id: i, | ||
set: (c) => { | ||
r = c; | ||
return s; | ||
}, removeSlice: (t) => { | ||
const s = typeof t == "string" ? [...e.values()].find((o) => o.name === t) : e.get(t.id); | ||
!s || e.delete(s.id); | ||
} }; | ||
}, M = (e) => Array.isArray(e) ? [...e] : typeof e == "object" ? { ...e } : e, L = (e, i) => { | ||
const r = Symbol(`Context-${i}`), t = (s, o = M(e)) => { | ||
let c = o; | ||
const d = { | ||
name: i, | ||
id: r, | ||
set: (u) => { | ||
c = u; | ||
}, | ||
get: () => r, | ||
update: (c) => { | ||
r = c(r); | ||
get: () => c, | ||
update: (u) => { | ||
c = u(c); | ||
} | ||
}; | ||
return n.set(i, m), m; | ||
return s.set(r, d), d; | ||
}; | ||
return t.sliceName = s, t.id = i, t._typeInfo = () => { | ||
throw T(); | ||
return t.sliceName = i, t.id = r, t._typeInfo = () => { | ||
throw P(); | ||
}, t; | ||
}; | ||
var g, p; | ||
class x { | ||
constructor(s, i) { | ||
class A { | ||
constructor(i, r) { | ||
h(this, g, void 0); | ||
h(this, p, void 0); | ||
this.use = (t) => o(this, g).getSlice(t), this.get = (t) => this.use(t).get(), this.set = (t, n) => this.use(t).set(n), this.update = (t, n) => this.use(t).update(n), this.timing = (t) => o(this, p).get(t), this.wait = (t) => this.timing(t)(), this.done = (t) => this.timing(t).done(), this.waitTimers = async (t) => { | ||
await Promise.all(this.get(t).map((n) => this.wait(n))); | ||
}, a(this, g, s), a(this, p, i); | ||
this.use = (t) => n(this, g).getSlice(t), this.get = (t) => this.use(t).get(), this.set = (t, s) => this.use(t).set(s), this.update = (t, s) => this.use(t).update(s), this.timing = (t) => n(this, p).get(t), this.done = (t) => this.timing(t).done(), this.wait = (t) => this.timing(t)(), this.waitTimers = async (t) => { | ||
await Promise.all(this.get(t).map((s) => this.wait(s))); | ||
}, a(this, g, i), a(this, p, r); | ||
} | ||
} | ||
g = new WeakMap(), p = new WeakMap(); | ||
var u, l; | ||
class L { | ||
constructor(s, i) { | ||
h(this, u, void 0); | ||
var m, l; | ||
class F { | ||
constructor(i, r) { | ||
h(this, m, void 0); | ||
h(this, l, void 0); | ||
this.inject = (t, n) => (t(o(this, u).sliceMap, n), this), this.record = (t) => (t(o(this, l).store), this), this.use = (t) => o(this, u).getSlice(t), this.get = (t) => this.use(t).get(), this.set = (t, n) => this.use(t).set(n), this.update = (t, n) => this.use(t).update(n), this.timing = (t) => o(this, l).get(t), this.wait = (t) => this.timing(t)(), this.done = (t) => this.timing(t).done(), this.waitTimers = async (t) => { | ||
await Promise.all(this.get(t).map((n) => this.wait(n))); | ||
}, a(this, u, s), a(this, l, i); | ||
this.inject = (t, s) => (t(n(this, m).sliceMap, s), this), this.remove = (t) => (n(this, m).removeSlice(t), this), this.record = (t) => (t(n(this, l).store), this), this.clearTimer = (t) => (n(this, l).remove(t), this), this.use = (t) => n(this, m).getSlice(t), this.get = (t) => this.use(t).get(), this.set = (t, s) => this.use(t).set(s), this.update = (t, s) => this.use(t).update(s), this.timing = (t) => n(this, l).get(t), this.wait = (t) => this.timing(t)(), this.done = (t) => this.timing(t).done(), this.waitTimers = async (t) => { | ||
await Promise.all(this.get(t).map((s) => this.wait(s))); | ||
}, a(this, m, i), a(this, l, r); | ||
} | ||
} | ||
u = new WeakMap(), l = new WeakMap(); | ||
var w, f; | ||
class A { | ||
constructor(s, i) { | ||
h(this, w, void 0); | ||
m = new WeakMap(), l = new WeakMap(); | ||
var v, f; | ||
class I { | ||
constructor(i, r) { | ||
h(this, v, void 0); | ||
h(this, f, void 0); | ||
this.inject = (t, n) => (t(o(this, w).sliceMap, n), this), this.record = (t) => (t(o(this, f).store), this), a(this, w, s), a(this, f, i); | ||
this.clearTimer = (t) => (n(this, f).remove(t), this), this.remove = (t) => (n(this, v).removeSlice(t), this), a(this, v, i), a(this, f, r); | ||
} | ||
} | ||
w = new WeakMap(), f = new WeakMap(); | ||
const F = () => { | ||
v = new WeakMap(), f = new WeakMap(); | ||
var w, y; | ||
class O { | ||
constructor(i, r) { | ||
h(this, w, void 0); | ||
h(this, y, void 0); | ||
this.inject = (t, s) => (t(n(this, w).sliceMap, s), this), this.record = (t) => (t(n(this, y).store), this), a(this, w, i), a(this, y, r); | ||
} | ||
} | ||
w = new WeakMap(), y = new WeakMap(); | ||
const $ = () => { | ||
const e = /* @__PURE__ */ new Map(); | ||
return { | ||
store: e, | ||
get: (i) => { | ||
const t = e.get(i.id); | ||
if (!t) | ||
throw M(i.name); | ||
return t; | ||
get: (t) => { | ||
const s = e.get(t.id); | ||
if (!s) | ||
throw k(t.timerName); | ||
return s; | ||
}, | ||
remove: (t) => { | ||
e.delete(t.id); | ||
} | ||
}; | ||
}, I = (e, s = 3e3) => { | ||
const i = Symbol("Timer"), t = (n) => { | ||
let d = null, r; | ||
const m = Symbol(e), c = () => d != null ? d : d = new Promise((y, C) => { | ||
r = (v) => { | ||
v instanceof CustomEvent && v.detail.id === m && (removeEventListener(e, r), v.stopImmediatePropagation(), y()); | ||
}, _ = (e, i = 3e3) => { | ||
const r = Symbol("Timer"), t = (s) => { | ||
let o = null, c; | ||
const d = Symbol(e), u = () => o != null ? o : o = new Promise((S, E) => { | ||
c = (C) => { | ||
C instanceof CustomEvent && C.detail.id === d && (removeEventListener(e, c), C.stopImmediatePropagation(), S()); | ||
}, setTimeout(() => { | ||
C(`Timing ${e} timeout.`), removeEventListener(e, r); | ||
}, s), addEventListener(e, r); | ||
E(`Timing ${e} timeout.`), removeEventListener(e, c); | ||
}, i), addEventListener(e, c); | ||
}); | ||
return c.done = () => { | ||
const y = new CustomEvent(e, { detail: { id: m } }); | ||
dispatchEvent(y); | ||
}, n.set(i, c), c; | ||
return u.done = () => { | ||
const S = new CustomEvent(e, { detail: { id: d } }); | ||
dispatchEvent(S); | ||
}, s.set(r, u), u; | ||
}; | ||
return t.id = i, t; | ||
return t.id = r, t.timerName = e, t; | ||
}; | ||
export { | ||
x as Ctx, | ||
L as Env, | ||
A as Pre, | ||
F as createClock, | ||
N as createContainer, | ||
j as createSlice, | ||
I as createTimer | ||
A as Ctx, | ||
F as Env, | ||
I as Post, | ||
O as Pre, | ||
$ as createClock, | ||
j as createContainer, | ||
L as createSlice, | ||
_ as createTimer | ||
}; | ||
//# sourceMappingURL=index.es.js.map |
@@ -52,2 +52,9 @@ import type { $Slice, Container, Slice } from '../context'; | ||
/** | ||
* Finish a timer | ||
* | ||
* @param timer - The timer needs to be finished. | ||
* @returns | ||
*/ | ||
readonly done: (timer: Timer) => void; | ||
/** | ||
* Wait for a timer to finish. | ||
@@ -60,9 +67,2 @@ * | ||
/** | ||
* Finish a timer | ||
* | ||
* @param timer - The timer needs to be finished. | ||
* @returns | ||
*/ | ||
readonly done: (timer: Timer) => void; | ||
/** | ||
* Wait for a list of timers in target slice to be all finished. | ||
@@ -69,0 +69,0 @@ * |
@@ -11,13 +11,27 @@ import { $Slice, Container, Slice } from '../context'; | ||
* @param defaultValue - The default value of this context. | ||
* @returns Pre. | ||
* @returns Env. | ||
*/ | ||
readonly inject: <T>(ctx: Slice<T, string>, defaultValue?: T | undefined) => this; | ||
/** | ||
* Remove a context from current editor. | ||
* | ||
* @param ctx - The context needs to be removed. | ||
* @returns Env. | ||
*/ | ||
readonly remove: <T, N extends string = string>(ctx: N | Slice<T, N>) => this; | ||
/** | ||
* Start to record for a timer. | ||
* | ||
* @param timer - The timer needs to be recorded. | ||
* @returns Pre. | ||
* @returns Env. | ||
*/ | ||
readonly record: (timer: Timer) => this; | ||
/** | ||
* Clear a timer record. | ||
* | ||
* @param timer - The timer needs to be cleared. | ||
* @returns Env. | ||
*/ | ||
readonly clearTimer: (timer: Timer) => this; | ||
/** | ||
* Get the slice instance. | ||
@@ -24,0 +38,0 @@ * |
export * from './ctx'; | ||
export * from './env'; | ||
export * from './post'; | ||
export * from './pre'; | ||
export * from './types'; | ||
//# sourceMappingURL=index.d.ts.map |
import type { Ctx } from './ctx'; | ||
import type { Post } from './post'; | ||
import type { Pre } from './pre'; | ||
export declare type CtxHandler = (ctx: Ctx) => void | Promise<void>; | ||
export declare type Cleanup = (post: Post) => void | Promise<void>; | ||
export declare type HandlerReturnType = void | Promise<void> | Cleanup | Promise<Cleanup>; | ||
export declare type CtxHandler = (ctx: Ctx) => HandlerReturnType; | ||
export declare type MilkdownPlugin = { | ||
@@ -5,0 +8,0 @@ (pre: Pre): CtxHandler; |
@@ -6,2 +6,3 @@ import { Timing } from './timing'; | ||
id: symbol; | ||
timerName: string; | ||
}; | ||
@@ -11,4 +12,5 @@ export declare type Clock = { | ||
get: (timer: Timer) => Timing; | ||
remove: (timer: Timer) => void; | ||
}; | ||
export declare const createClock: () => Clock; | ||
//# sourceMappingURL=clock.d.ts.map |
{ | ||
"name": "@milkdown/ctx", | ||
"version": "6.4.1", | ||
"version": "6.5.0", | ||
"type": "module", | ||
@@ -23,3 +23,3 @@ "main": "./lib/index.es.js", | ||
"dependencies": { | ||
"@milkdown/exception": "6.4.1", | ||
"@milkdown/exception": "6.5.0", | ||
"tslib": "^2.4.0" | ||
@@ -26,0 +26,0 @@ }, |
@@ -28,2 +28,16 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
}); | ||
it('removeSlice', () => { | ||
const container = createContainer(); | ||
const ctx = createSlice(0, 'num'); | ||
ctx(container.sliceMap); | ||
expect(container.getSlice(ctx).id).toBe(ctx.id); | ||
expect(container.getSlice(ctx).get()).toBe(0); | ||
container.removeSlice(ctx); | ||
expect(() => container.getSlice(ctx)).toThrow(); | ||
}); | ||
}); |
@@ -7,4 +7,5 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
export type Container = { | ||
readonly sliceMap: Map<symbol, $Slice>; | ||
readonly getSlice: <T, N extends string = string>(slice: Slice<T, N> | N) => $Slice<T, N>; | ||
readonly sliceMap: Map<symbol, $Slice>; | ||
readonly removeSlice: <T, N extends string = string>(slice: Slice<T, N> | N) => void; | ||
}; | ||
@@ -26,3 +27,12 @@ | ||
return { getSlice, sliceMap }; | ||
const removeSlice = <T, N extends string = string>(slice: Slice<T, N> | N): void => { | ||
const context = | ||
typeof slice === 'string' ? [...sliceMap.values()].find((x) => x.name === slice) : sliceMap.get(slice.id); | ||
if (!context) return; | ||
sliceMap.delete(context.id); | ||
}; | ||
return { sliceMap, getSlice, removeSlice }; | ||
}; |
@@ -24,3 +24,3 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
export const createSlice = <T, N extends string = string>(value: T, name: N): Slice<T, N> => { | ||
const id = Symbol('Context'); | ||
const id = Symbol(`Context-${name}`); | ||
@@ -27,0 +27,0 @@ const factory = (container: SliceMap, resetValue = shallowClone(value)) => { |
@@ -67,2 +67,10 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
/** | ||
* Finish a timer | ||
* | ||
* @param timer - The timer needs to be finished. | ||
* @returns | ||
*/ | ||
readonly done = (timer: Timer) => this.timing(timer).done(); | ||
/** | ||
* Wait for a timer to finish. | ||
@@ -76,10 +84,2 @@ * | ||
/** | ||
* Finish a timer | ||
* | ||
* @param timer - The timer needs to be finished. | ||
* @returns | ||
*/ | ||
readonly done = (timer: Timer) => this.timing(timer).done(); | ||
/** | ||
* Wait for a list of timers in target slice to be all finished. | ||
@@ -86,0 +86,0 @@ * |
@@ -19,3 +19,3 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
* @param defaultValue - The default value of this context. | ||
* @returns Pre. | ||
* @returns Env. | ||
*/ | ||
@@ -28,6 +28,17 @@ readonly inject = <T>(ctx: Slice<T>, defaultValue?: T) => { | ||
/** | ||
* Remove a context from current editor. | ||
* | ||
* @param ctx - The context needs to be removed. | ||
* @returns Env. | ||
*/ | ||
readonly remove = <T, N extends string = string>(ctx: Slice<T, N> | N) => { | ||
this.#container.removeSlice(ctx); | ||
return this; | ||
}; | ||
/** | ||
* Start to record for a timer. | ||
* | ||
* @param timer - The timer needs to be recorded. | ||
* @returns Pre. | ||
* @returns Env. | ||
*/ | ||
@@ -40,2 +51,13 @@ readonly record = (timer: Timer) => { | ||
/** | ||
* Clear a timer record. | ||
* | ||
* @param timer - The timer needs to be cleared. | ||
* @returns Env. | ||
*/ | ||
readonly clearTimer = (timer: Timer) => { | ||
this.#clock.remove(timer); | ||
return this; | ||
}; | ||
/** | ||
* Get the slice instance. | ||
@@ -42,0 +64,0 @@ * |
/* Copyright 2021, Milkdown by Mirone. */ | ||
export * from './ctx'; | ||
export * from './env'; | ||
export * from './post'; | ||
export * from './pre'; | ||
export * from './types'; |
/* Copyright 2021, Milkdown by Mirone. */ | ||
import type { Ctx } from './ctx'; | ||
import type { Post } from './post'; | ||
import type { Pre } from './pre'; | ||
export type CtxHandler = (ctx: Ctx) => void | Promise<void>; | ||
export type Cleanup = (post: Post) => void | Promise<void>; | ||
export type HandlerReturnType = void | Promise<void> | Cleanup | Promise<Cleanup>; | ||
export type CtxHandler = (ctx: Ctx) => HandlerReturnType; | ||
export type MilkdownPlugin = { | ||
(pre: Pre): CtxHandler; | ||
}; |
@@ -18,2 +18,12 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
}); | ||
it('remove', () => { | ||
const clock = createClock(); | ||
const timer = createTimer('timer'); | ||
timer(clock.store); | ||
expect(clock.get(timer)).toBe(clock.store.get(timer.id)); | ||
clock.remove(timer); | ||
expect(() => clock.get(timer)).toThrow(); | ||
}); | ||
}); |
@@ -11,2 +11,3 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
id: symbol; | ||
timerName: string; | ||
}; | ||
@@ -17,2 +18,3 @@ | ||
get: (timer: Timer) => Timing; | ||
remove: (timer: Timer) => void; | ||
}; | ||
@@ -24,10 +26,15 @@ | ||
const meta = store.get(timer.id); | ||
if (!meta) throw timerNotFound(timer.name); | ||
if (!meta) throw timerNotFound(timer.timerName); | ||
return meta; | ||
}; | ||
const remove = (timer: Timer) => { | ||
store.delete(timer.id); | ||
}; | ||
return { | ||
store, | ||
get, | ||
remove, | ||
}; | ||
}; |
@@ -46,4 +46,5 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
timer.id = id; | ||
timer.timerName = name; | ||
return timer; | ||
}; |
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
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59526
52
987
+ Added@milkdown/exception@6.5.0(transitive)
- Removed@milkdown/exception@6.4.1(transitive)
Updated@milkdown/exception@6.5.0