Skip to content

Commit

Permalink
fix timers primitives (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
smaeda-ks authored Oct 3, 2023
1 parent 21fa983 commit 4faf819
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
12 changes: 6 additions & 6 deletions packages/primitives/src/primitives/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ export function load(scopedContext = {}) {
})
assign(context, { console: consoleImpl.console })

/** @type {import('../../type-definitions/timer')} */
const timeoutImpl = requireWithFakeGlobalScope({
/** @type {import('../../type-definitions/timers')} */
const timersImpl = requireWithFakeGlobalScope({
context,
id: 'timeout.js',
sourceCode: injectSourceCode('./timer.js'),
id: 'timers.js',
sourceCode: injectSourceCode('./timers.js'),
scopedContext,
})
assign(context, {
setTimeout: timeoutImpl.setTimeout,
setInterval: timeoutImpl.setInterval,
setTimeout: timersImpl.setTimeout,
setInterval: timersImpl.setInterval,
})

/** @type {import('../../type-definitions/events')} */
Expand Down
10 changes: 0 additions & 10 deletions packages/primitives/src/primitives/timer.js

This file was deleted.

18 changes: 18 additions & 0 deletions packages/primitives/src/primitives/timers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const setTimeoutProxy = new Proxy(setTimeout, {
apply: (target, thisArg, args) => {
const timeout = Reflect.apply(target, thisArg, args)
// Returns integer value of timeout ID
return timeout[Symbol.toPrimitive]()
},
})

const setIntervalProxy = new Proxy(setInterval, {
apply: (target, thisArg, args) => {
const timeout = Reflect.apply(target, thisArg, args)
// Returns integer value of timeout ID
return timeout[Symbol.toPrimitive]()
},
})

export { setTimeoutProxy as setTimeout }
export { setIntervalProxy as setInterval }
2 changes: 1 addition & 1 deletion packages/primitives/type-definitions/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export * from './streams'
export * from './text-encoding-streams'
export * from './structured-clone'
export * from './url'
export * from './timer'
export * from './timers'
13 changes: 6 additions & 7 deletions packages/vm/tests/edge-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,15 +628,14 @@ describe('`Timers`', () => {
'%s function should return integer',
async (f) => {
const runtime = new EdgeVM()
expect(() => {
runtime.evaluate(`
runtime.evaluate(`
this.funcName = ${f}.name;
const timer = ${f}(() => {}, 1000);
timer.unref();
this.isNumber = (typeof timer === 'number');
clearTimeout(timer);
`)
}).toThrow({
name: 'Error',
message: `timer.unref is not a function`,
})
expect(runtime.context.funcName).toBe(f)
expect(runtime.context.isNumber).toBe(true)
},
)
})

1 comment on commit 4faf819

@vercel
Copy link

@vercel vercel bot commented on 4faf819 Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

edge-runtime – ./

edge-runtime.vercel.app
edge-runtime-git-main.vercel.sh
edge-runtime.vercel.sh

Please sign in to comment.