Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 52 additions & 40 deletions lib/scope-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,54 @@ import { createTask } from "./task.ts";

import type { Context, Operation, Scope, Task } from "./types.ts";

// Shared prototype so all scope instances have the same hidden class,
// avoiding deoptimization when V8 encounters differently-shaped scope objects.
const scopePrototype = {
[Symbol.toStringTag]: "Scope",
get<T>(this: ScopeInternal, context: Context<T>): T | undefined {
return (this.contexts[context.name] ?? context.defaultValue) as
| T
| undefined;
},
set<T>(this: ScopeInternal, context: Context<T>, value: T): T {
return this.contexts[context.name] = value;
},
expect<T>(this: ScopeInternal, context: Context<T>): T {
let value = this.get(context);
if (typeof value === "undefined") {
let error = new Error(context.name);
error.name = `MissingContextError`;
throw error;
}
return value;
},
delete<T>(this: ScopeInternal, context: Context<T>): boolean {
return delete this.contexts[context.name];
},
hasOwn<T>(this: ScopeInternal, context: Context<T>): boolean {
return !!Reflect.getOwnPropertyDescriptor(this.contexts, context.name);
},
run<T>(this: ScopeInternal, operation: () => Operation<T>): Task<T> {
return createTask({ owner: this, operation });
},
spawn<T>(
this: ScopeInternal,
operation: () => Operation<T>,
): Operation<Task<T>> {
// deno-lint-ignore no-this-alias
let owner = this;
return {
*[Symbol.iterator]() {
return createTask({ owner, operation });
},
};
},
ensure(this: ScopeInternal, op: () => Operation<void>): () => void {
this.destructors.add(op);
return () => this.destructors.delete(op);
},
};

export function createScopeInternal(
parent?: Scope,
): [ScopeInternal, () => Operation<void>] {
Expand All @@ -14,46 +62,9 @@ export function createScopeInternal(
let contexts: Record<string, unknown> = Object.create(
parent ? (parent as ScopeInternal).contexts : null,
);
let scope: ScopeInternal = Object.create({
[Symbol.toStringTag]: "Scope",
contexts,
get<T>(context: Context<T>): T | undefined {
return (contexts[context.name] ?? context.defaultValue) as T | undefined;
},
set<T>(context: Context<T>, value: T): T {
return contexts[context.name] = value;
},
expect<T>(context: Context<T>): T {
let value = scope.get(context);
if (typeof value === "undefined") {
let error = new Error(context.name);
error.name = `MissingContextError`;
throw error;
}
return value;
},
delete<T>(context: Context<T>): boolean {
return delete contexts[context.name];
},
hasOwn<T>(context: Context<T>): boolean {
return !!Reflect.getOwnPropertyDescriptor(contexts, context.name);
},
run<T>(operation: () => Operation<T>): Task<T> {
return createTask({ owner: scope, operation });
},
spawn<T>(operation: () => Operation<T>): Operation<Task<T>> {
return {
*[Symbol.iterator]() {
return createTask({ owner: scope, operation });
},
};
},

ensure(op: () => Operation<void>): () => void {
destructors.add(op);
return () => destructors.delete(op);
},
});
let scope: ScopeInternal = Object.create(scopePrototype);
scope.contexts = contexts;
scope.destructors = destructors;

scope.set(Priority, scope.expect(Priority) + 1);
scope.set(Children, new Set());
Expand Down Expand Up @@ -97,5 +108,6 @@ export function createScopeInternal(

export interface ScopeInternal extends Scope, AsyncDisposable {
contexts: Record<string, unknown>;
destructors: Set<() => Operation<void>>;
ensure(op: () => Operation<void>): () => void;
}
19 changes: 7 additions & 12 deletions www/assets/prism-atom-one-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ pre {
margin-right: -16px;
border-left: 4px solid
rgba(
0,
0,
0,
0
); /* Set placeholder for highlight accent border color to transparent */
0,
0,
0,
0
); /* Set placeholder for highlight accent border color to transparent */
}

.code-line.inserted {
Expand Down Expand Up @@ -275,10 +275,7 @@ pre[class*="language-"] {
color: var(--prism-hue-3);
}

.language-javascript
.token.template-string
> .token.interpolation
> .token.interpolation-punctuation.punctuation {
.language-javascript .token.template-string > .token.interpolation > .token.interpolation-punctuation.punctuation {
color: var(--prism-hue-5-2);
}

Expand Down Expand Up @@ -406,9 +403,7 @@ div.code-toolbar > .toolbar.toolbar > .toolbar-item > span:focus {

/* Hovering over a linkable line number (in the gutter area) */
/* Requires Line Numbers plugin as well */
pre[id].linkable-line-numbers.linkable-line-numbers
span.line-numbers-rows
> span:hover:before {
pre[id].linkable-line-numbers.linkable-line-numbers span.line-numbers-rows > span:hover:before {
background-color: var(--prism-syntax-cursor-line);
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading